/**
 * 通用模糊查询
 * @param {*} array 原数组 
 * @param {*} searchObj 搜索对象
 * @param {*} caseSensitive 区分大小写
 * @returns 
 */
export function fuzzyMatch(array, searchObj, caseSensitive = false)  {
    // 1. 验证输入
    if (!Array.isArray(array) || typeof searchObj !== 'object' || searchObj === null) {
        return [];
    }

    // 2. 预处理搜索条件
    const searchKeys = Object.keys(searchObj).filter(key => {
        const val = searchObj[key];
        return val !== undefined && val !== null && val !== '';
    });

    if (searchKeys.length === 0) return [...array];

    // 3. 执行筛选
    return array.filter(item => {
        if (typeof item !== 'object' || item === null) return false;

        return searchKeys.every(searchKey => {
            // 3.1 如果item没有这个属性，跳过该条件的检查（视为匹配）
            if (!item.hasOwnProperty(searchKey)) {
                return true; // 修改为返回true表示跳过检查
            }

            const itemValue = item[searchKey];
            const searchValue = searchObj[searchKey];

            // 3.2 处理null/undefined
            if (itemValue === null || itemValue === undefined) {
                return searchValue === null || searchValue === undefined;
            }

            // 3.3 自动类型匹配
            switch (typeof searchValue) {
                case 'number':
                    return Number(itemValue) === Number(searchValue);
                
                case 'boolean':
                    return Boolean(itemValue) === searchValue;
                
                default:
                    const itemStr = caseSensitive 
                        ? String(itemValue) 
                        : String(itemValue).toLowerCase();
                    const searchStr = caseSensitive
                        ? String(searchValue)
                        : String(searchValue).toLowerCase();
                    return itemStr.includes(searchStr);
            }
        });
    });
}